﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsGiaBan
    {
        private double mGiaBan = 0.0;
        private string mKhoaHangHoa = "";
        private string mKhoaLoaiGia = "";
        private string mKhoaNhanVienCapNhat = "";
        private string mLoaiTien = "";
        private string mNgayBatDau = "";

        public DataTable GetBangGia(string strKhoaLoaiGia, string strLanguage)
        {
            string commandText = " SELECT UPPER(NH.Ten" + strLanguage + ") As NhomHang, HH.Ma As MaHang, HH.TenViet, HH.TenAnh, HH.DienGiai, Rtrim(DVT.Ten" + strLanguage + ") + ' ' + Rtrim(HH.KichThuoc) As DonViTinh, Upper(LG.Ten" + strLanguage + ") As LoaiGia, GB.GiaBan  FROM DM_HangHoa HH left join DM_NhomHangHoa NH ON HH.KhoaNhom = NH.Khoa  LEFT JOIN DM_DonViTinh DVT on HH.KhoaDonViTinh = DVT.Khoa  left join BH_GiaBan GB on HH.Khoa = GB.KhoaHangHoa And GB.KhoaLoaiGia = '" + strKhoaLoaiGia + "'  left join DM_LoaiGia LG on GB.KhoaLoaiGia = LG.Khoa ORDER BY 1, 4 ";
            return DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
        }

        public DataTable GetDieuKhoanBaoGia()
        {
            string commandText = " SELECT NoiDung, NoiDung As DKBaoGia, HienThi FROM BH_DieuKhoanBaoGiaTemplate WHERE 0 = 1";
            return DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
        }

        public double GetGiaBan(string strKhoaHangHoa, string strKhoaLoaiGia)
        {
            double num;
            double num2 = 0.0;
            try
            {
                string commandText = "SELECT GiaBan FROM BH_GiaBan WHERE KhoaHangHoa = '" + strKhoaHangHoa.Trim() + "' And KhoaLoaiGia = '" + strKhoaLoaiGia.Trim() + "'";
                IDataReader dataReader = DBHelper.ExecuteReader(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                if (dataReader.Read())
                {
                    num2 = Conversions.ToDouble(modGeneral.GetValue(dataReader, 0, modGeneral.typeOfColumn.GetDecimal));
                }
                dataReader.Close();
                num = num2;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                num = 0.0;
                ProjectData.ClearProjectError();
                return num;
                ProjectData.ClearProjectError();
            }
            return num;
        }

        public DataTable GetList(string strLoaiTien, string strKhoaNhom = "", string strKhoaHangSanXuat = "")
        {
            DataTable table;
            IDataParameter[] commandParameters = new IDataParameter[4];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, strLoaiTien);
                commandParameters[1] = DBHelper.createParameter("@KhoaNhom", DbType.Int64, ParameterDirection.Input, strKhoaNhom);
                commandParameters[2] = DBHelper.createParameter("@KhoaHangSanXuat", DbType.Int64, ParameterDirection.Input, strKhoaHangSanXuat);
                commandParameters[3] = DBHelper.createParameter("@Language", DbType.Int64, ParameterDirection.Input, modGeneral.H_LANGUAGE);
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.StoredProcedure, "BH_sp_GetGiaBan", commandParameters).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool SaveData()
        {
            bool flag2;
            IDataParameter[] commandParameters = new IDataParameter[6];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@KhoaHangHoa", DbType.Int64, ParameterDirection.Input, this.mKhoaHangHoa);
                commandParameters[1] = DBHelper.createParameter("@KhoaLoaiGia", DbType.Int64, ParameterDirection.Input, this.mKhoaLoaiGia);
                commandParameters[2] = DBHelper.createParameter("@LoaiTien", DbType.Int64, ParameterDirection.Input, this.mLoaiTien);
                commandParameters[3] = DBHelper.createParameter("@GiaBan", DbType.Date, ParameterDirection.Input, this.mGiaBan);
                commandParameters[4] = DBHelper.createParameter("@NgayBatDau", DbType.Int64, ParameterDirection.Input, this.mNgayBatDau);
                commandParameters[5] = DBHelper.createParameter("@KhoaNhanVienCapNhat", DbType.Int64, ParameterDirection.Input, this.mKhoaNhanVienCapNhat);
                bool flag = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "BH_sp_CapNhatGiaBan", commandParameters) > 0;
                flag2 = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag2 = false;
                ProjectData.ClearProjectError();
                return flag2;
                ProjectData.ClearProjectError();
            }
            return flag2;
        }

        public double TinhGia(object strCongThuc)
        {
            string commandText = Conversions.ToString(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject(Microsoft.VisualBasic.CompilerServices.Operators.ConcatenateObject(" SELECT ", strCongThuc), " As Gia "));
            return Conversions.ToDouble(LVYModule.ReturnIfNull(RuntimeHelpers.GetObjectValue(DBHelper.ExecuteScalar(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0])), 0, modGeneral.TypeOfValue.Nothing));
        }

        public double GiaBan
        {
            get
            {
                return this.mGiaBan;
            }
            set
            {
                this.mGiaBan = value;
            }
        }

        public string KhoaHangHoa
        {
            get
            {
                return this.mKhoaHangHoa;
            }
            set
            {
                this.mKhoaHangHoa = value;
            }
        }

        public string KhoaLoaiGia
        {
            get
            {
                return this.mKhoaLoaiGia;
            }
            set
            {
                this.mKhoaLoaiGia = value;
            }
        }

        public string KhoaNhanVienCapNhat
        {
            get
            {
                return this.mKhoaNhanVienCapNhat;
            }
            set
            {
                this.mKhoaNhanVienCapNhat = value;
            }
        }

        public string LoaiTien
        {
            get
            {
                return this.mLoaiTien;
            }
            set
            {
                this.mLoaiTien = value;
            }
        }

        public string NgayBatDau
        {
            get
            {
                return this.mNgayBatDau;
            }
            set
            {
                this.mNgayBatDau = value;
            }
        }
    }
}

